/*********************************************************************************
 *      Copyright:  (C) 2020 YangZhengQing<2489833081@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  send_pack.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/06/2020)
 *         Author:  YangZhengQing <2489833081@qq.com>
 *      ChangeLog:  1, Release initial version on "04/06/2020 12:30:13 PM"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "../include/client.h"


/* 功能：将数据包发送至服务器 ，此函数阻塞等待服务器响应
 * 参数：sockfd:传入socket套接字
 *       pack_buff:传入数据包
 *       size:数据包的大小
 * 返回值：成功返回0，失败返回-1*/

int send_pack_to_server(int sockfd,unsigned char *pack_buff,int size)
{
    if(size == 1)
    {
        return -1;
    }

    if(write(sockfd,pack_buff,size) <0 )
    {

        printf("write to serve unsuccessfully: %s\n",strerror(errno));
        mylog(__func__,__LINE__,ERROR,"write to serve failed:%s",strerror(errno));
        return -1;
    }
    
    return 0;


}


/*功能：等待服务器响应
 * 参数：
 * 返回值:成功返回0,失败返回-1*/

int wait_response(int sockfd,resinfo *res)
{
    int                     rv = -1;
                                 
    if((rv = read(sockfd,res->resbuff,sizeof(res->resbuff))) < 0)
    {                            
        printf("read from serve unsuccessfully: %s\n",strerror(errno));
        mylog(__func__,__LINE__,ERROR,"read from serve failed:%s",strerror(errno));
        return -1;             
    }                            

    if(rv == 0)                  
    {                            
        printf("disconnect serve\n");
        mylog(__func__,__LINE__,ERROR,"disconnect failed:%s",strerror(errno));
        return -1;             
    }
    res->state = 1;//表示响应成功
    return 0;
}

